package com.tozed.android.launcher.simpleui;

import com.tozed.android.launcher.LauncherApplication;

import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Intent;
import android.util.Log;

public class SimpleLauncherAppWidgetInfo extends SimpleItemInfo
{
	private static boolean DEBUG = false;
	public static final int NO_ID = -1;
	static final String TAG = "SimpleLauncherAppWidgetInfo";
	public int appWidgetId = NO_ID;
	ComponentName providerName;
	public AppWidgetHostView hostView = null;
	
	SimpleLauncherAppWidgetInfo(int widgetId)
	{
		itemType = SimpleLauncherSettings.SimpleFavorites.ITEM_TYPE_APPWIDGET;
	    appWidgetId = widgetId;
	    //this.providerName = providerName;
	    
	    AppWidgetManager widgetManager = LauncherApplication.getActiveInstance().getAppWidgetManager();
	    if (widgetManager == null){
	      Log.i(TAG, "appWidgetManager is null");
	      return;
	    }
	    
	    AppWidgetProviderInfo widgetInfo = widgetManager.getAppWidgetInfo(appWidgetId);
	    this.intent = new Intent();
	    if (widgetInfo != null)
	    {
	    	 final String strPkg = widgetInfo.provider.getPackageName();
	 	    final String strCls = widgetInfo.provider.getClassName();
	 	    ComponentName component = new ComponentName(strPkg, strCls);
	 	    
	 	    if (DEBUG)
	 	      Log.i(TAG, "SimpleLauncherAppWidgetInfo pkg = " + strPkg + ", cls = " + strCls);
	 	    
	 	    intent.setComponent(component);
	    	
	    }else{
	    	Log.w(TAG, "the provider is null, and the appWidgetId is :" + appWidgetId);
	    }
	}
	
	SimpleLauncherAppWidgetInfo(int widgetId, ComponentName componentName)
	{
		itemType = SimpleLauncherSettings.SimpleFavorites.ITEM_TYPE_APPWIDGET;
	    appWidgetId = widgetId;
	    
	    AppWidgetManager widgetManager = LauncherApplication.getActiveInstance().getAppWidgetManager();
	    if (widgetManager == null){
	      Log.i(TAG, "appWidgetManager is null");
	      return;
	    }
	    
	    AppWidgetProviderInfo widgetInfo = widgetManager.getAppWidgetInfo(appWidgetId);
	    intent = new Intent();
	    ComponentName component = new ComponentName(widgetInfo.provider.getPackageName(), 
	    		widgetInfo.provider.getClassName());
	    intent.setComponent(component);
	    spanX = -1;
	    spanY = -1;
	}
	
	public SimpleLauncherAppWidgetInfo(ComponentName componentName){
		 itemType = 4;
		 spanX = -1;
		 spanY = -1;
	}
	
	void onAddToDatabase(ContentValues values)
	{
		super.onAddToDatabase(values);
		values.put("appWidgetId", Integer.valueOf(appWidgetId));
		values.put("intent", intent.toUri(0));
	}
	
	public String toString(){
		return "AppWidget(id=" + Integer.toString(appWidgetId) + " screen=" + this.screen 
				+ " cellX=" + this.cellX + " cellY=" + this.cellY 
				+ " spanX=" + this.spanX + " spanY=" + this.spanY 
				+ " isFixedPosition=" + this.isFixedPosition + ")";
	}

	void unbind(){
		super.unbind();
		hostView = null;
	}
}
